﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;
    using YidanSoft.Library.EditorUtility.Win32;

    [ToolboxItem(false)]
    public class CheckBoxEx : CheckBox
    {
        private const int CHECK_MARK_SIZE = 11;
        private Bitmap checkMarkChecked = null;
        private Bitmap checkMarkDisableChecked = null;
        private Bitmap checkMarkDisableUnchecked = null;
        private Bitmap checkMarkHotChecked = null;
        private Bitmap checkMarkHotUnchecked = null;
        private Bitmap checkMarkUnchecked = null;
        private DrawState drawState = DrawState.Normal;
        private Color oldControlDarkColor = Color.Empty;
        private Color oldHighLightColor = Color.Empty;

        public CheckBoxEx()
        {
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.checkMarkChecked = ResourceUtil.LoadBitmapResource(System.Type.GetType("YidanSoft.Library.EditorUtility.WinControls.CheckBoxEx"), "YidanSoft.Library.EditorUtility.Resources.Controls", "CheckMarkChecked");
            Debug.Assert(this.checkMarkChecked != null);
            this.checkMarkUnchecked = ResourceUtil.LoadBitmapResource(System.Type.GetType("YidanSoft.Library.EditorUtility.WinControls.CheckBoxEx"), "YidanSoft.Library.EditorUtility.Resources.Controls", "CheckMarkUnchecked");
            Debug.Assert(this.checkMarkUnchecked != null);
        }

        private Bitmap DoFiltering(Bitmap bitmap, Color oldColor, Color newColor)
        {
            Bitmap bitmap2 = (Bitmap) bitmap.Clone();
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color pixel = bitmap2.GetPixel(i, j);
                    if (((pixel.R == oldColor.R) && (pixel.G == oldColor.G)) && (pixel.B == oldColor.B))
                    {
                        bitmap2.SetPixel(i, j, newColor);
                    }
                }
            }
            return bitmap2;
        }

        private void DrawCheckBoxState(DrawState state)
        {
            this.FilterImages();
            Rectangle clientRectangle = base.ClientRectangle;
            IntPtr dC = WindowsAPI.GetDC(base.Handle);
            Rectangle rectangle2 = new Rectangle(clientRectangle.Left, clientRectangle.Top + ((clientRectangle.Height - 11) / 2), 11, 11);
            using (Graphics graphics = Graphics.FromHdc(dC))
            {
                if (state == DrawState.Normal)
                {
                    if (base.Checked)
                    {
                        graphics.DrawImage(this.checkMarkChecked, rectangle2.Left, rectangle2.Top);
                    }
                    else
                    {
                        graphics.DrawImage(this.checkMarkUnchecked, rectangle2.Left, rectangle2.Top);
                    }
                }
                else if (state == DrawState.Hot)
                {
                    if (base.Checked)
                    {
                        graphics.DrawImage(this.checkMarkHotChecked, rectangle2.Left, rectangle2.Top);
                    }
                    else
                    {
                        graphics.DrawImage(this.checkMarkHotUnchecked, rectangle2.Left, rectangle2.Top);
                    }
                }
                else if (state == DrawState.Disable)
                {
                    if (base.Checked)
                    {
                        graphics.DrawImage(this.checkMarkDisableChecked, rectangle2.Left, rectangle2.Top);
                    }
                    else
                    {
                        graphics.DrawImage(this.checkMarkDisableUnchecked, rectangle2.Left, rectangle2.Top);
                    }
                }
            }
            WindowsAPI.ReleaseDC(base.Handle, dC);
        }

        private void FilterImages()
        {
            if ((this.oldHighLightColor == Color.Empty) || (((this.oldHighLightColor.R != ColorUtil.VSNetBorderColor.R) || (this.oldHighLightColor.G != ColorUtil.VSNetBorderColor.G)) || (this.oldHighLightColor.B != ColorUtil.VSNetBorderColor.B)))
            {
                this.oldHighLightColor = ColorUtil.VSNetBorderColor;
                this.checkMarkHotChecked = this.DoFiltering(this.checkMarkChecked, Color.Black, ColorUtil.VSNetBorderColor);
                this.checkMarkHotUnchecked = this.DoFiltering(this.checkMarkUnchecked, Color.Black, ColorUtil.VSNetBorderColor);
            }
            if ((this.oldControlDarkColor == Color.Empty) || (((this.oldHighLightColor.R != ColorUtil.VSNetBorderColor.R) || (this.oldHighLightColor.G != ColorUtil.VSNetBorderColor.G)) || (this.oldHighLightColor.B != ColorUtil.VSNetBorderColor.B)))
            {
                this.oldControlDarkColor = SystemColors.ControlDark;
                this.checkMarkDisableChecked = this.DoFiltering(this.checkMarkChecked, Color.Black, SystemColors.ControlDark);
                this.checkMarkDisableUnchecked = this.DoFiltering(this.checkMarkUnchecked, Color.Black, SystemColors.ControlDark);
            }
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.drawState = DrawState.Hot;
            base.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.drawState = DrawState.Normal;
            base.Invalidate();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.drawState = DrawState.Hot;
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (!base.ContainsFocus)
            {
                this.drawState = DrawState.Normal;
                base.Invalidate();
            }
        }

        protected override void WndProc(ref Message m)
        {
            bool flag = true;
            if (m.Msg == 15)
            {
                base.WndProc(ref m);
                this.DrawCheckBoxState(base.Enabled ? this.drawState : DrawState.Disable);
                flag = false;
            }
            if (flag)
            {
                base.WndProc(ref m);
            }
        }

        public System.Windows.Forms.FlatStyle FlatStyle
        {
            get
            {
                return base.FlatStyle;
            }
            set
            {
                if (value != System.Windows.Forms.FlatStyle.Flat)
                {
                    string paramName = "FlatStyle needs to be set to Flat for this class";
                    ArgumentException exception = new ArgumentException("FlatStyle", paramName);
                    throw exception;
                }
                base.FlatStyle = value;
            }
        }
    }
}

